/**
 * @License: (C) Copyright 2013-2022
 * @Contact: abcque@outlook.com
 * @IDE: IntelliJ IDEA
 * @Encoding: utf-8
 * @Project: st2214 2215
 * @Package: PACKAGE_NAME
 * @File: YanghuiTriangle.java
 * @Time: 2022/12/16 10:25
 * @Author: abcque
 * @Description: 使用二维数组处理杨辉三角
 * @Modifier:
 * @ModifyDate:
 * @ModifyRemark:
 */
public class YanghuiTriangle {
    public static void main(String[] args) {
        // step1、创建杨辉三角数组，数组名称为 triangle。
        int[][] triangle;
        triangle = new int[10][];
        for (int i = 0; i < triangle.length; i++) {
            triangle[i] = new int[i + 1];
        }

        // step2、将每行的第一个和最后一个元素置一
        for (int i = 0; i < triangle.length; i++) {
            triangle[i][0] = 1;
            triangle[i][triangle[i].length - 1] = 1;
        }

        // step3、处理每行的中间元素
        for (int i = 2; i < triangle.length; i++) {
            for (int j = 1; j < triangle[i].length - 1; j++) {
                triangle[i][j] = triangle[i - 1][j] + triangle[i - 1][j - 1];
            }
        }

        // step 、显示杨辉三角形
        for (int i = 0; i < triangle.length; i++) {
            for (int j = 0; j < triangle[i].length; j++) {
                System.out.printf("%3d ", triangle[i][j]);
            }
            System.out.println();
        }
    }
}
